package com.deyun.panelcabinet.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.deyun.panelcabinet.R;
import com.deyun.panelcabinet.entity.TestEntity;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class TestAdapter extends BaseAdapter {

    private List<TestEntity> list = null;
    private Activity mContext;
    private LayoutInflater mInflater;

    public TestAdapter(List<TestEntity> list, Activity mContext) {
        this.list = list;
        this.mContext = mContext;
        mInflater = LayoutInflater.from(mContext);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        final ViewHolder viewHolder;
        final TestEntity testEntity = list.get(position);
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.lv_item_user_info, viewGroup, false);
            viewHolder = new ViewHolder(convertView);
            // holder.mTextView = (TextView)convertView.findViewById(R.id.tv_title);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.tv_username.setText(testEntity.getName());

        if (testEntity.isCheck()) {
            viewHolder.cb_select.setChecked(true);
        } else {
            viewHolder.cb_select.setChecked(false);
        }

        if (testEntity.isException()) {
            viewHolder.cb_exception.setChecked(true);
        } else {
            viewHolder.cb_exception.setChecked(false);
        }

        viewHolder.cb_select.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (viewHolder.cb_select.isChecked()) {
                    testEntity.setCheck(true);
                } else {
                    testEntity.setCheck(false);
                }
            }
        });


        viewHolder.cb_exception.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (viewHolder.cb_exception.isChecked()) {
                    testEntity.setException(true);
                } else {
                    testEntity.setException(false);
                }
            }
        });


        return convertView;
    }

    static class ViewHolder {
        @BindView(R.id.cb_select)
        CheckBox cb_select;
        @BindView(R.id.cb_exception)
        CheckBox cb_exception;
        @BindView(R.id.tv_username)
        TextView tv_username;


        public ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
